import {
  findChildFormDetails,
  getEditorProvider,
  IEditorController,
  IEditorProvider,
  isValueChange,
} from '@ibiz-template/runtime';
import { IDEFormDetail, IDEFormItem } from '@ibiz/model-core';
import { FormMDCtrlContentTypeController } from '../../form-mdctrl-content-type.controller';

/**
 * 表单多数据部件表格类型控制器
 *
 * @author zk
 * @date 2023-07-28 08:07:57
 * @export
 * @class FormMDCtrlGridController
 * @extends {EditFormController}
 * @implements {IFormController}
 */
export class FormMDCtrlRepeaterGridController extends FormMDCtrlContentTypeController {
  /**
   * 表格列对象
   *
   * @author zk
   * @date 2023-07-31 12:07:58
   * @type {{ codeName: string; caption: string }[]}
   * @memberof FormMDCtrlRepeaterGridController
   */
  columns: { codeName: string; caption: string }[] = [];

  /**
   * 编辑器对象
   *
   * @author zk
   * @date 2023-07-31 12:07:48
   * @type {{
   *     [key: string]: { provider: IEditorProvider; controller: IEditorController };
   *   }}
   * @memberof FormMDCtrlRepeaterGridController
   */
  editorMap: {
    [key: string]: { provider: IEditorProvider; controller: IEditorController };
  } = {};

  /**
   * 初始化
   *
   * @author zk
   * @date 2023-07-25 10:07:11
   * @protected
   * @return {*}  {Promise<void>}
   * @memberof FormMDCtrlController
   */
  public async onInit(): Promise<void> {
    this.initColumns(this.model.deformDetails || []);
  }

  /**
   * 初始化表格列
   *
   * @author zk
   * @date 2023-07-31 12:07:06
   * @param {IDEFormDetail[]} details
   * @memberof FormMDCtrlRepeaterGridController
   */
  public initColumns(details: IDEFormDetail[]): void {
    details.forEach(async detail => {
      if (detail.detailType === 'FORMITEM') {
        const editor = (detail as IDEFormItem).editor;
        const editorProvider = await getEditorProvider(editor!);
        if (editorProvider) {
          const c = await editorProvider.createController(editor!, {
            unitName: (editor as IDEFormItem).unitName,
            valueFormat: (editor as IDEFormItem).valueFormat,
            context: this.parent.form.context,
            params: this.parent.form.params,
          });
          this.editorMap[detail.codeName!] = {
            provider: editorProvider,
            controller: c,
          };
        }
        this.columns.push({
          codeName: detail.codeName!,
          caption: detail.caption!,
        });
      }
      const childDetails = findChildFormDetails(detail);
      if (childDetails.length) {
        this.initColumns(childDetails);
      }
    });
  }

  /**
   * 行数据改变
   *
   * @author zk
   * @date 2023-07-30 10:07:27
   * @param {unknown} val
   * @param {string} name
   * @param {number} index
   * @return {*}
   * @memberof FormMDCtrlRepeaterGridController
   */
  rowDataChange(val: unknown, name: string, index: number): void {
    const item = this.data[index];
    if (
      Object.prototype.hasOwnProperty.call(item, name) &&
      !isValueChange(item[name], val)
    ) {
      // `表格行数据里没有属性${name}或者${name}的值未发生改变`
      return;
    }
    item[name] = val;
    this.onDataItemChange(item, index);
  }
}
